package com.laiketui.admin.mch.service.plugin;

import com.laiketui.admin.mch.api.plugin.AuctionService;
import com.laiketui.common.api.PubliceService;
import com.laiketui.common.api.plugin.PubliceAuctionService;
import com.laiketui.common.mapper.*;
import com.laiketui.common.utils.tool.data.DataCheckTool;
import com.laiketui.common.utils.tool.data.GoodsDataUtils;
import com.laiketui.common.utils.tool.str.StringUtils;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.core.utils.tool.DataUtils;
import com.laiketui.core.utils.tool.DateUtil;
import com.laiketui.core.utils.tool.SerializePhpUtils;
import com.laiketui.domain.config.ConfiGureModel;
import com.laiketui.domain.lktconst.DictionaryConst;
import com.laiketui.domain.lktconst.ErrorCode;
import com.laiketui.domain.order.OrderModel;
import com.laiketui.domain.plugin.auction.AuctionPromiseModel;
import com.laiketui.domain.plugin.auction.AuctionRecordModel;
import com.laiketui.domain.plugin.jp.AuctionConfigModel;
import com.laiketui.domain.product.AuctionProductModel;
import com.laiketui.domain.product.ProductListModel;
import com.laiketui.domain.vo.MainVo;
import com.laiketui.domain.vo.plugin.auction.AddAuctionConfigVo;
import com.laiketui.domain.vo.plugin.auction.AddAuctionVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 竞拍后台管理
 *
 * @author Trick
 * @date 2021/5/14 12:03
 */
@Service
public class AuctionServiceImpl implements AuctionService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AuctionProductModelMapper auctionProductModelMapper;

    @Autowired
    private PubliceService publiceService;

    @Autowired
    private ConfiGureModelMapper confiGureModelMapper;

    @Autowired
    private ProductListModelMapper productListModelMapper;

    @Autowired
    private AuctionConfigModelMapper auctionConfigModelMapper;

    @Autowired
    private AuctionRecordModelMapper auctionRecordModelMapper;

    @Autowired
    private OrderModelMapper orderModelMapper;

    @Autowired
    private OrderDetailsModelMapper orderDetailsModelMapper;

    @Autowired
    private AuctionPromiseModelMapper auctionPromiseModelMapper;

    @Autowired
    private PubliceAuctionService publiceAuctionService;

    @Override
    public Map<String, Object> index(MainVo vo, String goodsName, Integer status, Integer id) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", vo.getStoreId());
            parmaMap.put("type", "admin");
            parmaMap.put("title", goodsName);
            parmaMap.put("id", id);
            if (status != null) {
                List<Integer> statusList = new ArrayList<>();
                if (status.equals(StatusEnum.HAS_END) || status.equals(StatusEnum.STREAMED)) {
                    statusList.add(2);
                    statusList.add(3);
                } else {
                    statusList.add(status);
                }
                parmaMap.put("statusList", statusList);
            }
            List<Map<String, Object>> auctionList = auctionProductModelMapper.selectAuctionGoodsList(parmaMap);
            int total = auctionProductModelMapper.countAuctionGoodsList(parmaMap);
            for (Map<String, Object> map : auctionList) {
                map.put("image", publiceService.getImgPath(map.get("imgurl").toString(), vo.getStoreId()));
            }
            resultMap.put("list", auctionList);
            resultMap.put("total", total);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("竞拍商品首页数据 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "index");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> auctionDetail(MainVo vo, int id) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            resultMap.putAll(index(vo, null, null, id));
            List<Map<String, Object>> auctionInfo = DataUtils.cast(resultMap.get("list"));
            if (auctionInfo != null) {
                for (Map<String, Object> map : auctionInfo) {
                    String attribute = map.get("attribute").toString();
                    int goodsId = 0;
                    int attrId = 0;
                    Map<Integer, Map<Integer, Object>> attrMap = SerializePhpUtils.getAuctionProductAttribute(attribute);
                    if (attrMap != null) {
                        for (Integer key : attrMap.keySet()) {
                            goodsId = key;
                            Map<Integer, Object> attr = attrMap.get(key);
                            for (Integer attrIdKey : attr.keySet()) {
                                attrId = attrIdKey;
                                break;
                            }
                            break;
                        }
                    }
                    //获取商品信息
                    Map<String, Object> parmaMap = new HashMap<>(16);
                    parmaMap.put("store_id", vo.getStoreId());
                    parmaMap.put("goodsId", goodsId);
                    parmaMap.put("attr_id", attrId);
                    List<Map<String, Object>> goodsList = confiGureModelMapper.getProductListLeftJoinMchDynamic(parmaMap);
                    for (Map<String, Object> goods : goodsList) {
                        String attrStr = GoodsDataUtils.getProductSkuValue(goods.get("attribute").toString());
                        map.put("attr", attrStr);
                    }
                }
            }
            resultMap.clear();
            resultMap.put("res", auctionInfo);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("编辑竞拍商品-加载数据 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "auctionDetail");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getAuctionGoods(MainVo vo, Integer classId, Integer brandId, String title) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", vo.getStoreId());
            parmaMap.put("stockNum", "stockNum");
            parmaMap.put("product_title", title);
            parmaMap.put("bargain", "bargain");
            parmaMap.put("classLeve", classId);
            parmaMap.put("brand_id", brandId);
            parmaMap.put("active", DictionaryConst.GoodsActive.GOODSACTIVE_POSITIVE_JP);
            List<Map<String, Object>> goodsList = productListModelMapper.getProductListDynamic(parmaMap);
            int total = productListModelMapper.countProductListDynamic(parmaMap);
            for (Map<String, Object> map : goodsList) {
                map.put("pro_img", publiceService.getImgPath(map.get("img").toString(), vo.getStoreId()));
                map.put("attrtype", GoodsDataUtils.getProductSkuValue(map.get("attribute").toString()));
            }
            resultMap.put("product_list", goodsList);
            resultMap.put("total", total);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("编辑竞拍商品-加载数据 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getAuctionGoods");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> addAuction(AddAuctionVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            int row = 0;
            //校验表单数据
            DataCheckTool.checkFreightDataFormate(vo);
            //获取商品规格信息
            ConfiGureModel confiGureModel = confiGureModelMapper.selectByPrimaryKey(vo.getAttrIdCheck());
            if (confiGureModel == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_GGBCZ, "规格不存在");
            }
            ProductListModel productListModel = productListModelMapper.selectByPrimaryKey(confiGureModel.getPid());
            if (productListModel == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPBCZ, "商品不存在");
            }
            //获取配置信息
            AuctionConfigModel auctionConfigModel = new AuctionConfigModel();
            auctionConfigModel.setStore_id(vo.getStoreId());
            auctionConfigModel = auctionConfigModelMapper.selectOne(auctionConfigModel);
            if (auctionConfigModel == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_JPPZWSZ, "竞拍配置未设置");
            }
            AuctionProductModel auctionProductSave = new AuctionProductModel();
            auctionProductSave.setStore_id(vo.getStoreId());
            auctionProductSave.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
            auctionProductSave.setTitle(vo.getTitle());
            auctionProductSave.setStarttime(DateUtil.dateFormateToDate(vo.getStarttime(), GloabConst.TimePattern.YMDHMS));
            auctionProductSave.setEndtime(DateUtil.dateFormateToDate(vo.getEndtime(), GloabConst.TimePattern.YMDHMS));
            auctionProductSave.setPrice(vo.getPrice());
            auctionProductSave.setAdd_price(vo.getAddPrice());
            auctionProductSave.setMarket_price(confiGureModel.getPrice());
            auctionProductSave.setCurrent_price(vo.getPrice());
            auctionProductSave.setImgurl(productListModel.getImgurl());
            Map<Integer, Map<Integer, Object>> attrMap = new HashMap<>(16);
            Map<Integer, Object> map = new HashMap<>(16);
            map.put(confiGureModel.getId(), confiGureModel.getId());
            attrMap.put(productListModel.getId(), map);
            auctionProductSave.setAttribute(SerializePhpUtils.JavaSerializeByPhp(attrMap));
            auctionProductSave.setDays(auctionConfigModel.getDays());
            auctionProductSave.setInvalid_time(DateUtil.getAddDate(auctionProductSave.getEndtime(), auctionConfigModel.getDays()));
            auctionProductSave.setPromise(vo.getPromise());
            auctionProductSave.setLow_pepole(auctionConfigModel.getLow_pepole());
            auctionProductSave.setWait_time(auctionConfigModel.getWait_time());
            auctionProductSave.setMch_id(productListModel.getMch_id());
            auctionProductSave.setIs_show(vo.getIsShow());

            AuctionProductModel auctionProductOld = null;
            if (vo.getId() != null) {
                auctionProductOld = auctionProductModelMapper.selectByPrimaryKey(vo.getId());
            }
            AuctionProductModel auctionProductModel = new AuctionProductModel();
            auctionProductModel.setStore_id(vo.getStoreId());
            auctionProductModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
            auctionProductModel.setTitle(vo.getTitle());
            if (auctionProductOld != null) {
                if (!auctionProductOld.getTitle().equals(vo.getTitle())) {
                    int count = auctionProductModelMapper.selectCount(auctionProductModel);
                    if (count > 0) {
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_JPBTBNZF, "竞拍标题不能重复");
                    }
                    row = auctionProductModelMapper.updateByPrimaryKeySelective(auctionProductSave);
                }
            } else {
                int count = auctionProductModelMapper.selectCount(auctionProductModel);
                if (count > 0) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_JPBTBNZF, "竞拍标题不能重复");
                }
                row = auctionProductModelMapper.insertSelective(auctionProductSave);
            }

            if (row < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加/编辑竞拍商品信息 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "addAuction");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> showSwitch(MainVo vo, Integer id) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            AuctionProductModel auctionProductOld = auctionProductModelMapper.selectByPrimaryKey(id);
            if (auctionProductOld == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSCW, "参数错误");
            }
            AuctionProductModel auctionProductUpdate = new AuctionProductModel();
            auctionProductUpdate.setId(auctionProductOld.getId());
            int isShow = 0;
            if (auctionProductOld.getIs_show() == DictionaryConst.WhetherMaven.WHETHER_NO) {
                isShow = 1;
            }
            auctionProductUpdate.setIs_show(isShow);
            auctionProductModelMapper.updateByPrimaryKeySelective(auctionProductUpdate);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("竞拍是否显示开关 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "showSwitch");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> getAcutionDetail(MainVo vo, int id, String name, Integer userId) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", vo.getStoreId());
            parmaMap.put("id", id);
            parmaMap.put("pageStart", vo.getPageNo());
            parmaMap.put("pageEnd", vo.getPageSize());
            parmaMap.put("id_sort", DataUtils.Sort.DESC.toString());
            parmaMap.put("add_time_sort", DataUtils.Sort.DESC.toString());
            parmaMap.put("price_sort", DataUtils.Sort.DESC.toString());
            List<Map<String, Object>> resBid = auctionRecordModelMapper.selectAuctionRecordInfo(parmaMap);
            for (Map<String, Object> map : resBid) {
                map.put("user_name", map.get("user_name"));
                map.put("add_time", DateUtil.dateFormate(map.get("add_time") + "", GloabConst.TimePattern.YMDHMS));
                if (!StringUtils.isEmpty(map.get("imgurl"))) {
                    map.put("image", publiceService.getImgPath(map.get("imgurl").toString(), vo.getStoreId()));
                }
            }
            //查询出竞拍商品状态，以及是否付款
            AuctionProductModel auctionProductModel = new AuctionProductModel();
            auctionProductModel.setStore_id(vo.getStoreId());
            auctionProductModel.setId(id);
            auctionProductModel = auctionProductModelMapper.selectOne(auctionProductModel);
            if (auctionProductModel != null) {
                resultMap.put("status", auctionProductModel.getStatus());
                resultMap.put("is_buy", auctionProductModel.getIs_buy());
                resultMap.put("pepole", auctionProductModel.getPepole());
                resultMap.put("low_pepole", auctionProductModel.getLow_pepole());
            }

            resultMap.put("list", resBid);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取保证金记录 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getPromiseInfo");
        }
        return resultMap;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> del(MainVo vo, String ids) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            int row;
            if (StringUtils.isEmpty(ids)) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSCW, "参数错误");
            }
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", vo.getStoreId());
            parmaMap.put("idList", ids.split(","));
            List<Map<String, Object>> resBid = auctionProductModelMapper.selectDynamic(parmaMap);
            for (Map<String, Object> map : resBid) {
                int id = Integer.parseInt(map.get("id").toString());
                int isBuy = Integer.parseInt(map.get("is_buy").toString());
                int status = Integer.parseInt(map.get("status").toString());
                String orderno = map.get("trade_no") + "";
                if (status == StatusEnum.IN_START || isBuy != DictionaryConst.WhetherMaven.WHETHER_NO) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSBCZZZJPSP, "操作失败，存在正在竞拍商品");
                }
                if (!StringUtils.isEmpty(orderno)) {
                    OrderModel orderModel = new OrderModel();
                    orderModel.setStore_id(vo.getStoreId());
                    orderModel.setsNo(orderno);
                    orderModel = orderModelMapper.selectOne(orderModel);
                    if (orderModel == null) {
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSBCZZZJPSP, "操作失败，存在正在竞拍商品");
                    }
                    OrderModel orderUpdate = new OrderModel();
                    orderUpdate.setId(orderModel.getId());
                    orderUpdate.setStatus(DictionaryConst.OrdersStatus.ORDERS_R_STATUS_CLOSE);
                    row = orderModelMapper.updateByPrimaryKeySelective(orderUpdate);
                    if (row < 1) {
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
                    }
                    row = orderDetailsModelMapper.updateOrderDetailsStatus(vo.getStoreId(), orderno, DictionaryConst.OrdersStatus.ORDERS_R_STATUS_CLOSE);
                    if (row < 1) {
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
                    }
                }
                AuctionProductModel auctionProductModel = new AuctionProductModel();
                auctionProductModel.setId(id);
                auctionProductModel.setRecycle(DictionaryConst.ProductRecycle.RECOVERY);
                row = auctionProductModelMapper.updateByPrimaryKeySelective(auctionProductModel);
                if (row < 1) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败");
                }
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除竞拍活动 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "del");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> siwtch(MainVo vo, Integer id) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            int row;
            if (id == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSCW, "参数错误");
            }
            //获取竞拍活动信息
            AuctionProductModel auctionProductModel = auctionProductModelMapper.selectByPrimaryKey(id);
            if (auctionProductModel == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_JPBCZ, "竞拍不存在");
            }
            AuctionProductModel auctionProductUpdate = new AuctionProductModel();
            auctionProductUpdate.setId(auctionProductModel.getId());
            if (auctionProductModel.getStatus().equals(StatusEnum.NOT_START)) {
                auctionProductUpdate.setStarttime(new Date());
                auctionProductUpdate.setStatus(StatusEnum.IN_START);
            } else {
                //获取竞拍设置信息
                AuctionConfigModel auctionConfigModel = new AuctionConfigModel();
                auctionConfigModel.setStore_id(vo.getStoreId());
                auctionConfigModel = auctionConfigModelMapper.selectOne(auctionConfigModel);
                if (auctionConfigModel == null) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_JPPZWSZ, "竞拍配置未设置");
                }
                //失效日期
                Date invalidDate = DateUtil.getAddDate(new Date(), auctionConfigModel.getDays());
                //获取最高得主
                Map<String, String> auctionMap = auctionRecordModelMapper.selectMaxUser(vo.getStoreId(), auctionProductModel.getId());
                //活动是否有出价
                AuctionRecordModel auctionRecordModel = new AuctionRecordModel();
                auctionRecordModel.setStore_id(vo.getStoreId());
                auctionRecordModel.setAuction_id(auctionProductModel.getId());
                int count = auctionRecordModelMapper.selectCount(auctionRecordModel);
                if (auctionProductModel.getPepole() < auctionProductModel.getLow_pepole() || count < 1) {
                    //结束人数不达标竞拍商品
                    auctionProductUpdate.setEndtime(new Date());
                    auctionProductUpdate.setInvalid_time(invalidDate);
                    auctionProductUpdate.setStatus(StatusEnum.STREAMED);
                } else {
                    //竞拍产品转态改变
                    auctionProductUpdate.setStatus(StatusEnum.HAS_END);
                    auctionProductUpdate.setEndtime(new Date());
                    auctionProductUpdate.setInvalid_time(invalidDate);
                    if (auctionMap != null) {
                        auctionProductUpdate.setCurrent_price(new BigDecimal(auctionMap.get("price")));
                        auctionProductUpdate.setUser_id(auctionMap.get("user_id"));
                        //获取保证金
                        AuctionPromiseModel auctionPromiseModel = new AuctionPromiseModel();
//                        auctionPromiseModel.setA_id(auctionProductModel.getId());
                        auctionPromiseModel.setUser_id(auctionMap.get("user_id"));
                        auctionPromiseModel = auctionPromiseModelMapper.selectOne(auctionPromiseModel);
                        if (auctionPromiseModel != null) {
                            //设置不符合退款
                            AuctionPromiseModel auctionPromiseUpdate = new AuctionPromiseModel();
                            auctionPromiseUpdate.setId(auctionPromiseModel.getId());
                            auctionPromiseUpdate.setAllow_back(DictionaryConst.WhetherMaven.WHETHER_NO);
                            auctionPromiseModelMapper.updateByPrimaryKeySelective(auctionPromiseUpdate);
                        }
                        //生成订单
                        publiceAuctionService.createOrder(vo.getStoreId(), vo.getStoreType(), auctionProductModel.getId(), auctionMap.get("user_id"), "");
                    }
                }
            }
            row = auctionProductModelMapper.updateByPrimaryKeySelective(auctionProductUpdate);
            if (row < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_XGSB, "修改失败");
            }
            // TODO: 2021/5/17 微信推送 尊敬的会员恭喜你竞拍xx成功 /n 请在规定时间内付款
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("开始,结束活动 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "siwtch");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> promiseList(MainVo vo, String userName, Integer type, String startDate, String endDate) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", vo.getStoreId());
            parmaMap.put("is_pay", DictionaryConst.WhetherMaven.WHETHER_NO);
            parmaMap.put("user_name", userName);
            parmaMap.put("type", type);
            parmaMap.put("add_time_start", startDate);
            parmaMap.put("add_time_end", endDate);
            parmaMap.put("pageStart", vo.getPageNo());
            parmaMap.put("pageEnd", vo.getPageSize());
            parmaMap.put("id_sort", DataUtils.Sort.DESC.toString());
            List<Map<String, Object>> list = auctionPromiseModelMapper.selectAuctionPromiseList(parmaMap);
            int total = auctionPromiseModelMapper.countAuctionPromiseList(parmaMap);

            resultMap.put("list", list);
            resultMap.put("total", total);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("保证金列表 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "promiseList");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> config(MainVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            AuctionConfigModel auctionConfigModel = new AuctionConfigModel();
            auctionConfigModel.setStore_id(vo.getStoreId());
            auctionConfigModel = auctionConfigModelMapper.selectOne(auctionConfigModel);
            if (auctionConfigModel != null) {
                resultMap.put("days", auctionConfigModel.getDays());
                resultMap.put("is_open", auctionConfigModel.getIs_open());
                resultMap.put("content", auctionConfigModel.getContent());
                resultMap.put("wait_time", auctionConfigModel.getWait_time());
                resultMap.put("low_pepole", auctionConfigModel.getLow_pepole());
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取竞拍设置 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "config");
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> addConfig(AddAuctionConfigVo vo) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            int row;
            //最低人数
            int lowPeople = 1;
            //最小时间(秒)
            int lowTime = 5;
            //最大时间(秒)
            int bigTime = 30;
            //最小天数
            int lowDay = 0;
            if (vo.getLowPepole() == null || vo.getLowPepole() <= lowPeople) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_ZDRSBNXY, "最低人数不能小于1");
            }
            if (vo.getWaitTime() == null || vo.getWaitTime() <= lowTime) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_DDSJBNXY, "等待时间不能小于5s");
            }
            if (vo.getWaitTime() >= bigTime) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_DDSJBNDY, "等待时间不能大于30s");
            }
            if (vo.getDays() == null || vo.getDays() <= lowDay) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_BLTSBNXY, "保留天数不能小于0");
            }
            if (StringUtils.isEmpty(vo.getAgreeContent())) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.PARAMATER_ERROR, "协议内容不能为空");
            }
            if (StringUtils.isEmpty(vo.getAgreeTitle())) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.PARAMATER_ERROR, "协议标题不能为空");
            }

            AuctionConfigModel auctionConfigSave = new AuctionConfigModel();
            auctionConfigSave.setContent(vo.getContent());
            auctionConfigSave.setIs_open(vo.getIsOpen());
            auctionConfigSave.setDays(vo.getDays());
            auctionConfigSave.setLow_pepole(vo.getLowPepole());
            auctionConfigSave.setWait_time(vo.getWaitTime());
            auctionConfigSave.setAgree_content(vo.getAgreeContent());
            auctionConfigSave.setAgree_title(vo.getAgreeTitle());

            AuctionConfigModel auctionConfigOld = new AuctionConfigModel();
            auctionConfigOld.setStore_id(vo.getStoreId());
            auctionConfigOld = auctionConfigModelMapper.selectOne(auctionConfigOld);
            if (auctionConfigOld != null) {
                auctionConfigSave.setId(auctionConfigOld.getId());
                row = auctionConfigModelMapper.updateByPrimaryKeySelective(auctionConfigSave);
            } else {
                auctionConfigSave.setStore_id(vo.getStoreId());
                row = auctionConfigModelMapper.insertSelective(auctionConfigSave);
            }

            if (row < 1) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_XGSB, "修改失败");
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("添加/编辑竞拍配置 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "addConfig");
        }
        return resultMap;
    }
}

